package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.NursingPlan;
import com.itheima.domain.PageResult;
import com.itheima.domain.Result;
import com.itheima.domain.dto.NursingPlanDto;
import com.itheima.domain.dto.NursingPlanQueryDto;
import com.itheima.service.NursingPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/nursingPlan")
@RequiredArgsConstructor
public class NursingPlanController {
    private final NursingPlanService nursingPlanService;

    @PostMapping("/add")
    public Result add(@RequestBody NursingPlanDto nursingPlanDto){
        boolean isSuccess = nursingPlanService.add(nursingPlanDto);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("添加失败");
        }
    }

    @PostMapping("/page")
    public Result page(@RequestBody NursingPlanQueryDto nursingPlanQueryDto){
        Page<NursingPlan> page = new Page<>(nursingPlanQueryDto.getCurrentPage(),nursingPlanQueryDto.getPageSize());

        String planName = nursingPlanQueryDto.getPlanName();
        Integer status = nursingPlanQueryDto.getStatus();

        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(planName != "", NursingPlan::getPlanName,planName)
                .eq(status != null, NursingPlan::getStatus,status);
        page = nursingPlanService.page(page,queryWrapper);

        // 判断查询结果
        if (page != null && page.getRecords() != null) {
            return Result.success(new PageResult<NursingPlan>(1,page.getRecords(),(int)page.getTotal()));
        } else {
            return Result.error("查询失败");
        }

    }



    @GetMapping("/allList")
    public List<NursingPlan> getAll(){
        List<NursingPlan> list = nursingPlanService.list();
        return list;
    }
}
